﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModels.Common;
using GenericTools;
using GenericTools.Models;

namespace ReportingManagement.Components.AutoReminder
{
    public partial class SelectTargetSMSReceivers : Form
    {
        private Dictionary<int, bool> currentFacilityRegisteredUsers = new Dictionary<int,bool>();

        public SelectTargetSMSReceivers()
        {
            InitializeComponent();

            DataTable dataTable = Utilities.GetAllFacilitiesStatus();
            lstFacilities.DataSource = dataTable;
            lstFacilities.DisplayMember = "Facility Name";
        }

        private void lstFacilities_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView row = lstFacilities.SelectedItem as DataRowView;
            if (row != null)
            {
                string facilityCode = row["Facility Code"].ToString();
                lblFacilityCode.Text = facilityCode;
                lblFacilityName.Text = row["Facility Name"].ToString();
                lblFacilityDistrict.Text = row["District Name"].ToString();
                lblFacilityRegion.Text = row["Region Name"].ToString();

                DataTable dataTable = Utilities.GetAllSMSUsersByFacility(facilityCode);
                dgvRegisteredUsers.AutoGenerateColumns = false;
                dgvRegisteredUsers.DataSource = dataTable;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            List<string> receivingNumbers = new List<string>();
            foreach (DataGridViewRow r in dgvRegisteredUsers.Rows)
            {
                bool canReceive = bool.Parse(r.Cells[0].Value.ToString());
                if (canReceive)
                {
                    string number = r.Cells[2].Value.ToString();
                    receivingNumbers.Add(number);
                }
            }

            if (receivingNumbers.Count < 1)
            {
                MessageBox.Show(this, "You must have at least one user who will receive SMS Reminder in the facility!",
                    AppConfiguration.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            string facilityCode = lblFacilityCode.Text;

            lstFacilities.Enabled = false;
            dgvRegisteredUsers.Enabled = false;
            btnSave.Enabled = false;
            string saveButtonText = btnSave.Text;
            btnReset.Enabled = false;
            btnSave.Text = "Please wait...";

            Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;
            Utilities.SaveReminderReceiver(facilityCode, receivingNumbers);
            string saveDate = DateTime.Now.ToString("s");
            Utilities.SetSetting(AutoReminderSettings.AUTO_REMINDER_SETTING_SAVE_DATE, saveDate);
            
            lstFacilities.Enabled = true;
            dgvRegisteredUsers.Enabled = true;
            btnReset.Enabled = true;
            btnSave.Text = saveButtonText;
            btnSave.Enabled = true;
            this.Cursor = Cursors.Default;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            lstFacilities.Enabled = false;
            dgvRegisteredUsers.Enabled = false;
            btnSave.Enabled = false;
            string resetButtonText = btnReset.Text;
            btnReset.Enabled = false;
            btnReset.Text = "Please wait...";

            Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;
            lstFacilities_SelectedIndexChanged(null, null);

            lstFacilities.Enabled = true;
            dgvRegisteredUsers.Enabled = true;
            btnReset.Enabled = true;
            btnReset.Text = resetButtonText;
            btnSave.Enabled = true;
            this.Cursor = Cursors.Default;            
        }

        //private void dgvRegisteredUsers_RowLeave(object sender, DataGridViewCellEventArgs e)
        //{
        //    DataGridViewRow row = dgvRegisteredUsers.Rows[e.RowIndex];
        //    bool canReceive = bool.Parse(row.Cells[0].Value.ToString());
        //    currentFacilityRegisteredUsers[e.RowIndex] = canReceive;

        //    btnSave.Enabled = false;
        //    Application.DoEvents();
        //    for (int i = 0; i < dgvRegisteredUsers.Rows.Count; i++)
        //    {
        //        DataGridViewRow r = dgvRegisteredUsers.Rows[i];
        //        bool currentCanReceiveValue = bool.Parse(r.Cells[0].Value.ToString());
        //        bool oldCanReceiveValue = currentFacilityRegisteredUsers[i];
        //        if (currentCanReceiveValue != oldCanReceiveValue)
        //        {
        //            btnSave.Enabled = true;
        //            break;
        //        }
        //    }
        //}
    }
}
